documentation
[lhc/web/wiklou.git] / includes / PageHistory.php
1 <?php
2 /**
3 * Page history
4 *
5 * Split off from Article.php and Skin.php, 2003-12-22
6 * @package MediaWiki
7 */
8
9 define('DIR_PREV', 0);
10 define('DIR_NEXT', 1);
11
12 /**
13 * This class handles printing the history page for an article. In order to
14 * be efficient, it uses timestamps rather than offsets for paging, to avoid
15 * costly LIMIT,offset queries.
16 *
17 * Construct it by passing in an Article, and call $h->history() to print the
18 * history.
19 *
20 * @package MediaWiki
21 */
22
23 class PageHistory {
24 var $mArticle, $mTitle, $mSkin;
25 var $lastdate;
26 var $linesonpage;
27 var $mNotificationTimestamp;
28 var $mLatestId = null;
29
30 /**
31 * Construct a new PageHistory.
32 *
33 * @param Article $article
34 * @returns nothing
35 */
36 function PageHistory($article) {
37 global $wgUser;
38
39 $this->mArticle =& $article;
40 $this->mTitle =& $article->mTitle;
41 $this->mNotificationTimestamp = NULL;
42 $this->mSkin = $wgUser->getSkin();
43
44 $this->defaultLimit = 50;
45 }
46
47 /**
48 * Print the history page for an article.
49 *
50 * @returns nothing
51 */
52 function history() {
53 global $wgOut, $wgRequest, $wgTitle;
54
55 /*
56 * Allow client caching.
57 */
58
59 if( $wgOut->checkLastModified( $this->mArticle->getTimestamp() ) )
60 /* Client cache fresh and headers sent, nothing more to do. */
61 return;
62
63 $fname = 'PageHistory::history';
64 wfProfileIn( $fname );
65
66 /*
67 * Setup page variables.
68 */
69 $wgOut->setPageTitle( $this->mTitle->getPrefixedText() );
70 $wgOut->setSubtitle( wfMsg( 'revhistory' ) );
71 $wgOut->setArticleFlag( false );
72 $wgOut->setArticleRelated( true );
73 $wgOut->setRobotpolicy( 'noindex,nofollow' );
74
75 /*
76 * Fail if article doesn't exist.
77 */
78 if( !$this->mTitle->exists() ) {
79 $wgOut->addWikiText( wfMsg( 'nohistory' ) );
80 wfProfileOut( $fname );
81 return;
82 }
83
84 $dbr =& wfGetDB(DB_SLAVE);
85
86 /*
87 * Extract limit, the number of revisions to show, and
88 * offset, the timestamp to begin at, from the URL.
89 */
90 $limit = $wgRequest->getInt('limit', $this->defaultLimit);
91 $offset = $wgRequest->getText('offset');
92
93 /* Offset must be an integral. */
94 if (!strlen($offset) || !preg_match("/^[0-9]+$/", $offset))
95 $offset = 0;
96 # $offset = $dbr->timestamp($offset);
97 $dboffset = $offset === 0 ? 0 : $dbr->timestamp($offset);
98 /*
99 * "go=last" means to jump to the last history page.
100 */
101 if (($gowhere = $wgRequest->getText("go")) !== NULL) {
102 switch ($gowhere) {
103 case "first":
104 if (($lastid = $this->getLastOffsetForPaging($this->mTitle->getArticleID(), $limit)) === NULL)
105 break;
106 $gourl = $wgTitle->getLocalURL("action=history&limit={$limit}&offset=".
107 wfTimestamp(TS_MW, $lastid));
108 break;
109 default:
110 $gourl = NULL;
111 }
112
113 if (!is_null($gourl)) {
114 $wgOut->redirect($gourl);
115 return;
116 }
117 }
118
119 /*
120 * Fetch revisions.
121 *
122 * If the user clicked "previous", we retrieve the revisions backwards,
123 * then reverse them. This is to avoid needing to know the timestamp of
124 * previous revisions when generating the URL.
125 */
126 $direction = $this->getDirection();
127 $revisions = $this->fetchRevisions($limit, $dboffset, $direction);
128 $navbar = $this->makeNavbar($revisions, $offset, $limit, $direction);
129
130 /*
131 * We fetch one more revision than needed to get the timestamp of the
132 * one after this page (and to know if it exists).
133 *
134 * linesonpage stores the actual number of lines.
135 */
136 if (count($revisions) < $limit + 1)
137 $this->linesonpage = count($revisions);
138 else
139 $this->linesonpage = count($revisions) - 1;
140
141 /* Un-reverse revisions */
142 if ($direction == DIR_PREV)
143 $revisions = array_reverse($revisions);
144
145 /*
146 * Print the top navbar.
147 */
148 $s = $navbar;
149 $s .= $this->beginHistoryList();
150 $counter = 1;
151
152 /*
153 * Print each revision, excluding the one-past-the-end, if any.
154 */
155 foreach (array_slice($revisions, 0, $limit) as $i => $line) {
156 $latest = !$i && $offset == 0;
157 $firstInList = !$i;
158 $next = isset( $revisions[$i + 1] ) ? $revisions[$i + 1 ] : null;
159 $s .= $this->historyLine($line, $next, $counter, $this->getNotificationTimestamp(), $latest, $firstInList);
160 $counter++;
161 }
162
163 /*
164 * End navbar.
165 */
166 $s .= $this->endHistoryList();
167 $s .= $navbar;
168
169 $wgOut->addHTML( $s );
170 wfProfileOut( $fname );
171 }
172
173 /** @todo document */
174 function beginHistoryList() {
175 global $wgTitle;
176 $this->lastdate = '';
177 $s = wfMsgWikiHtml( 'histlegend' );
178 $s .= '<form action="' . $wgTitle->escapeLocalURL( '-' ) . '" method="get">';
179 $prefixedkey = htmlspecialchars($wgTitle->getPrefixedDbKey());
180
181 // The following line is SUPPOSED to have double-quotes around the
182 // $prefixedkey variable, because htmlspecialchars() doesn't escape
183 // single-quotes.
184 //
185 // On at least two occasions people have changed it to single-quotes,
186 // which creates invalid HTML and incorrect display of the resulting
187 // link.
188 //
189 // Please do not break this a third time. Thank you for your kind
190 // consideration and cooperation.
191 //
192 $s .= "<input type='hidden' name='title' value=\"{$prefixedkey}\" />\n";
193
194 $s .= $this->submitButton();
195 $s .= '<ul id="pagehistory">' . "\n";
196 return $s;
197 }
198
199 /** @todo document */
200 function endHistoryList() {
201 $last = wfMsg( 'last' );
202
203 $s = '</ul>';
204 $s .= $this->submitButton( array( 'id' => 'historysubmit' ) );
205 $s .= '</form>';
206 return $s;
207 }
208
209 /** @todo document */
210 function submitButton( $bits = array() ) {
211 return ( $this->linesonpage > 0 )
212 ? wfElement( 'input', array_merge( $bits,
213 array(
214 'class' => 'historysubmit',
215 'type' => 'submit',
216 'accesskey' => wfMsgHtml( 'accesskey-compareselectedversions' ),
217 'title' => wfMsgHtml( 'tooltip-compareselectedversions' ),
218 'value' => wfMsgHtml( 'compareselectedversions' ),
219 ) ) )
220 : '';
221 }
222
223 /** @todo document */
224 function historyLine( $row, $next, $counter = '', $notificationtimestamp = false, $latest = false, $firstInList = false ) {
225 global $wgUser;
226 $rev = new Revision( $row );
227
228 $s = '<li>';
229 $curlink = $this->curLink( $rev, $latest );
230 $lastlink = $this->lastLink( $rev, $next, $counter );
231 $arbitrary = $this->diffButtons( $rev, $firstInList, $counter );
232 $link = $this->revLink( $rev );
233 $user = $this->mSkin->revUserLink( $rev );
234
235 $s .= "($curlink) ($lastlink) $arbitrary";
236
237 if( $wgUser->isAllowed( 'deleterevision' ) ) {
238 $revdel = Title::makeTitle( NS_SPECIAL, 'Revisiondelete' );
239 if( $firstInList ) {
240 // We don't currently handle well changing the top revision's settings
241 $del = wfMsgHtml( 'rev-delundel' );
242 } else {
243 $del = $this->mSkin->makeKnownLinkObj( $revdel,
244 wfMsg( 'rev-delundel' ),
245 'target=' . urlencode( $this->mTitle->getPrefixedDbkey() ) .
246 '&oldid=' . urlencode( $rev->getId() ) );
247 }
248 $s .= "(<small>$del</small>) ";
249 }
250
251 $s .= " $link <span class='history-user'>$user</span>";
252
253 if( $row->rev_minor_edit ) {
254 $s .= ' ' . wfElement( 'span', array( 'class' => 'minor' ), wfMsgHtml( 'minoreditletter') );
255 }
256
257 $s .= $this->mSkin->revComment( $rev );
258 if ($notificationtimestamp && ($row->rev_timestamp >= $notificationtimestamp)) {
259 $s .= ' <span class="updatedmarker">' . wfMsgHtml( 'updatedmarker' ) . '</span>';
260 }
261 if( $row->rev_deleted & MW_REV_DELETED_TEXT ) {
262 $s .= ' ' . wfMsgHtml( 'deletedrev' );
263 }
264 $s .= "</li>\n";
265
266 return $s;
267 }
268
269 /** @todo document */
270 function revLink( $rev ) {
271 global $wgLang;
272 $date = $wgLang->timeanddate( wfTimestamp(TS_MW, $rev->getTimestamp()), true );
273 if( $rev->userCan( MW_REV_DELETED_TEXT ) ) {
274 $link = $this->mSkin->makeKnownLinkObj(
275 $this->mTitle, $date, "oldid=" . $rev->getId() );
276 } else {
277 $link = $date;
278 }
279 if( $rev->isDeleted( MW_REV_DELETED_TEXT ) ) {
280 return '<span class="history-deleted">' . $link . '</span>';
281 }
282 return $link;
283 }
284
285 /** @todo document */
286 function curLink( $rev, $latest ) {
287 $cur = wfMsgHtml( 'cur' );
288 if( $latest || !$rev->userCan( MW_REV_DELETED_TEXT ) ) {
289 return $cur;
290 } else {
291 return $this->mSkin->makeKnownLinkObj(
292 $this->mTitle, $cur,
293 'diff=' . $this->getLatestID() .
294 "&oldid=" . $rev->getId() );
295 }
296 }
297
298 /** @todo document */
299 function lastLink( $rev, $next, $counter ) {
300 $last = htmlspecialchars( wfMsg( 'last' ) );
301 if( is_null( $next ) ) {
302 if( $rev->getTimestamp() == $this->getEarliestOffset() ) {
303 return $last;
304 } else {
305 // Cut off by paging; there are more behind us...
306 return $this->mSkin->makeKnownLinkObj(
307 $this->mTitle,
308 $last,
309 "diff=" . $rev->getId() . "&oldid=prev" );
310 }
311 } elseif( !$rev->userCan( MW_REV_DELETED_TEXT ) ) {
312 return $last;
313 } else {
314 return $this->mSkin->makeKnownLinkObj(
315 $this->mTitle,
316 $last,
317 "diff=" . $rev->getId() . "&oldid={$next->rev_id}"
318 /*,
319 '',
320 '',
321 "tabindex={$counter}"*/ );
322 }
323 }
324
325 /** @todo document */
326 function diffButtons( $rev, $firstInList, $counter ) {
327 if( $this->linesonpage > 1) {
328 $radio = array(
329 'type' => 'radio',
330 'value' => $rev->getId(),
331 # do we really need to flood this on every item?
332 # 'title' => wfMsgHtml( 'selectolderversionfordiff' )
333 );
334
335 if( !$rev->userCan( MW_REV_DELETED_TEXT ) ) {
336 $radio['disabled'] = 'disabled';
337 }
338
339 /** @todo: move title texts to javascript */
340 if ( $firstInList ) {
341 $first = wfElement( 'input', array_merge(
342 $radio,
343 array(
344 'style' => 'visibility:hidden',
345 'name' => 'oldid' ) ) );
346 $checkmark = array( 'checked' => 'checked' );
347 } else {
348 if( $counter == 2 ) {
349 $checkmark = array( 'checked' => 'checked' );
350 } else {
351 $checkmark = array();
352 }
353 $first = wfElement( 'input', array_merge(
354 $radio,
355 $checkmark,
356 array( 'name' => 'oldid' ) ) );
357 $checkmark = array();
358 }
359 $second = wfElement( 'input', array_merge(
360 $radio,
361 $checkmark,
362 array( 'name' => 'diff' ) ) );
363 return $first . $second;
364 } else {
365 return '';
366 }
367 }
368
369 /** @todo document */
370 function getLatestOffset( $id = null ) {
371 if ( $id === null) $id = $this->mTitle->getArticleID();
372 return $this->getExtremeOffset( $id, 'max' );
373 }
374
375 /** @todo document */
376 function getEarliestOffset( $id = null ) {
377 if ( $id === null) $id = $this->mTitle->getArticleID();
378 return $this->getExtremeOffset( $id, 'min' );
379 }
380
381 /** @todo document */
382 function getExtremeOffset( $id, $func ) {
383 $db =& wfGetDB(DB_SLAVE);
384 return $db->selectField( 'revision',
385 "$func(rev_timestamp)",
386 array( 'rev_page' => $id ),
387 'PageHistory::getExtremeOffset' );
388 }
389
390 /** @todo document */
391 function getLatestId() {
392 if( is_null( $this->mLatestId ) ) {
393 $id = $this->mTitle->getArticleID();
394 $db =& wfGetDB(DB_SLAVE);
395 $this->mLatestId = $db->selectField( 'revision',
396 "max(rev_id)",
397 array( 'rev_page' => $id ),
398 'PageHistory::getLatestID' );
399 }
400 return $this->mLatestId;
401 }
402
403 /** @todo document */
404 function getLastOffsetForPaging( $id, $step ) {
405 $fname = 'PageHistory::getLastOffsetForPaging';
406
407 $dbr =& wfGetDB(DB_SLAVE);
408 $res = $dbr->select(
409 'revision',
410 'rev_timestamp',
411 "rev_page=$id",
412 $fname,
413 array('ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => $step));
414
415 $n = $dbr->numRows( $res );
416 $last = null;
417 while( $obj = $dbr->fetchObject( $res ) ) {
418 $last = $obj->rev_timestamp;
419 }
420 $dbr->freeResult( $res );
421 return $last;
422 }
423
424 /**
425 * @return returns the direction of browsing watchlist
426 */
427 function getDirection() {
428 global $wgRequest;
429 if ($wgRequest->getText("dir") == "prev")
430 return DIR_PREV;
431 else
432 return DIR_NEXT;
433 }
434
435 /** @todo document */
436 function fetchRevisions($limit, $offset, $direction) {
437 $fname = 'PageHistory::fetchRevisions';
438
439 $dbr =& wfGetDB( DB_SLAVE );
440
441 if ($direction == DIR_PREV)
442 list($dirs, $oper) = array("ASC", ">=");
443 else /* $direction == DIR_NEXT */
444 list($dirs, $oper) = array("DESC", "<=");
445
446 if ($offset)
447 $offsets = array("rev_timestamp $oper '$offset'");
448 else
449 $offsets = array();
450
451 $page_id = $this->mTitle->getArticleID();
452
453 $res = $dbr->select(
454 'revision',
455 array('rev_id', 'rev_page', 'rev_text_id', 'rev_user', 'rev_comment', 'rev_user_text',
456 'rev_timestamp', 'rev_minor_edit', 'rev_deleted'),
457 array_merge(array("rev_page=$page_id"), $offsets),
458 $fname,
459 array('ORDER BY' => "rev_timestamp $dirs",
460 'USE INDEX' => 'page_timestamp', 'LIMIT' => $limit)
461 );
462
463 $result = array();
464 while (($obj = $dbr->fetchObject($res)) != NULL)
465 $result[] = $obj;
466
467 return $result;
468 }
469
470 /** @todo document */
471 function getNotificationTimestamp() {
472 global $wgUser, $wgShowUpdatedMarker;
473 $fname = 'PageHistory::getNotficationTimestamp';
474
475 if ($this->mNotificationTimestamp !== NULL)
476 return $this->mNotificationTimestamp;
477
478 if ($wgUser->isAnon() || !$wgShowUpdatedMarker)
479 return $this->mNotificationTimestamp = false;
480
481 $dbr =& wfGetDB(DB_SLAVE);
482
483 $this->mNotificationTimestamp = $dbr->selectField(
484 'watchlist',
485 'wl_notificationtimestamp',
486 array( 'wl_namespace' => $this->mTitle->getNamespace(),
487 'wl_title' => $this->mTitle->getDBkey(),
488 'wl_user' => $wgUser->getID()
489 ),
490 $fname);
491
492 return $this->mNotificationTimestamp;
493 }
494
495 /** @todo document */
496 function makeNavbar($revisions, $offset, $limit, $direction) {
497 global $wgLang;
498
499 $revisions = array_slice($revisions, 0, $limit);
500
501 $latestTimestamp = wfTimestamp(TS_MW, $this->getLatestOffset());
502 $earliestTimestamp = wfTimestamp(TS_MW, $this->getEarliestOffset());
503
504 /*
505 * When we're displaying previous revisions, we need to reverse
506 * the array, because it's queried in reverse order.
507 */
508 if ($direction == DIR_PREV)
509 $revisions = array_reverse($revisions);
510
511 /*
512 * lowts is the timestamp of the first revision on this page.
513 * hights is the timestamp of the last revision.
514 */
515
516 $lowts = $hights = 0;
517
518 if( count( $revisions ) ) {
519 $latestShown = wfTimestamp(TS_MW, $revisions[0]->rev_timestamp);
520 $earliestShown = wfTimestamp(TS_MW, $revisions[count($revisions) - 1]->rev_timestamp);
521 }
522
523 /* Don't announce the limit everywhere if it's the default */
524 $usefulLimit = $limit == $this->defaultLimit ? '' : $limit;
525
526 $urls = array();
527 foreach (array(20, 50, 100, 250, 500) as $num) {
528 $urls[] = $this->MakeLink( $wgLang->formatNum($num),
529 array('offset' => $offset == 0 ? '' : wfTimestamp(TS_MW, $offset), 'limit' => $num, ) );
530 }
531
532 $bits = implode($urls, ' | ');
533
534 wfDebug("latestShown=$latestShown latestTimestamp=$latestTimestamp\n");
535 if( $latestShown < $latestTimestamp ) {
536 $prevtext = $this->MakeLink( wfMsgHtml("prevn", $limit),
537 array( 'dir' => 'prev', 'offset' => $latestShown, 'limit' => $usefulLimit ) );
538 $lasttext = $this->MakeLink( wfMsgHtml('histlast'),
539 array( 'limit' => $usefulLimit ) );
540 } else {
541 $prevtext = wfMsgHtml("prevn", $limit);
542 $lasttext = wfMsgHtml('histlast');
543 }
544
545 wfDebug("earliestShown=$earliestShown earliestTimestamp=$earliestTimestamp\n");
546 if( $earliestShown > $earliestTimestamp ) {
547 $nexttext = $this->MakeLink( wfMsgHtml("nextn", $limit),
548 array( 'offset' => $earliestShown, 'limit' => $usefulLimit ) );
549 $firsttext = $this->MakeLink( wfMsgHtml('histfirst'),
550 array( 'go' => 'first', 'limit' => $usefulLimit ) );
551 } else {
552 $nexttext = wfMsgHtml("nextn", $limit);
553 $firsttext = wfMsgHtml('histfirst');
554 }
555
556 $firstlast = "($lasttext | $firsttext)";
557
558 return "$firstlast " . wfMsgHtml("viewprevnext", $prevtext, $nexttext, $bits);
559 }
560
561 function MakeLink($text, $query = NULL) {
562 if ( $query === null ) return $text;
563 return $this->mSkin->makeKnownLinkObj(
564 $this->mTitle, $text,
565 wfArrayToCGI( $query, array( 'action' => 'history' )));
566 }
567
568
569 }
570
571 ?>